package com.hlm.controller;

import com.hlm.domain.Home;
import com.hlm.domain.User;
import com.hlm.domain.UserHome;
import com.hlm.service.HlmUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/hlm")
public class HlmController {
    @Resource
    private HlmUserService service;

    @RequestMapping("/addUser.do")
    //添加客户
    public ModelAndView addUser(User user){

        ModelAndView mv = new ModelAndView();
        String tips = "添加失败";
        int nums = service.insertUser(user);
        if(nums > 0){
            //注册成功
            tips = "客户【"+user.getName()+"】 添加成功";
        }
        //添加数据
        mv.addObject("tips",tips);
        //指定结果页面
        mv.setViewName("result");
        return mv;
    }

    //添加房间
    @RequestMapping("/addHome.do")
    public ModelAndView addHome(Home home){
        ModelAndView mv = new ModelAndView();
        String tips = "添加失败";
        int nums = service.insertHome(home);
        if(nums > 0){
            //注册成功
            tips = "客户【"+home.getKefangbianhao()+"】 添加成功";
        }
        //添加数据
        mv.addObject("tips",tips);
        //指定结果页面
        mv.setViewName("addHome");
        return mv;
    }


    //查房间
    @RequestMapping("/queryHome.do")
    @ResponseBody
    public List<Home> queryHome(){
        List<Home> homes = service.selectHome();
        return homes;
    }
@RequestMapping(value ="/addUserHome.do")
    //客户入住
    public ModelAndView addUserHome(UserHome userHome){
        ModelAndView  mv = new ModelAndView();
        //修改房间状态
        int num   = service.updateHome(userHome.getKefangbianhao(),userHome.getShiyongzhuangtai());
        String tips = "房间修改失败，";
        String tips2 = "客户入住失败。";
        if(num > 0){
            //修改房间成功
             tips = "房间修改成功，";
            //客户入住
            int nums = service.insertUserHome(userHome);
            if(nums > 0){
                //客户成功入住
                tips2 = "客户入住成功。";
            }
        }

        //为结果页面添加数据
        mv.addObject("tips",tips);
        mv.addObject("tips2",tips2);
        //指定结果页面
        mv.setViewName("addUserHome");
        return mv;
    }
}
